<?php

namespace Home\Controller;

class GroupController extends CommController
{
    public function index()
    {
        $this->assign("info", $this->uinfos);
        $this->display();
    }

    public function useraction($id)
    {
        $saleagent = M("saleagent");
        $saleagent->create();
        if ($saleagent->where("sag_id=" . $id)->save()) {
            $this->redirect("Group/index");
        } else {
            $this->redirect("Group/index");
        }
    }

    public function report($start = null, $end = null, $sal = null)
    {
        $salestaff = M("salestaff");
        $couponhis = M("couponhis");
        $deallogs = M("deallogs");

        $child = $salestaff->where("ssa_agid=" . $this->uinfos['sag_id'])->select();
        $id_child = i_array_column($child, "ssa_id");
        $sal = $sal == 1 ? null : $sal;
        $couwhere["cou_disid"] = $sal != null ? $sal : array("in", implode(",", $id_child));
        $ordlist = $couponhis->where($couwhere)->select();
        $id_ord = i_array_column($ordlist, "cou_orders");
        $where["dll_ordsn"] = array("in", implode(",", $id_ord));
        $start != null && $end != null ? $where["dll_time"] = array("between", array(strtotime($start), strtotime($end))) : null;
        $list = $id_ord != null ? getpages($deallogs, $where, 10, "dll_time desc") : null;
        $this->assign("list", $list);
        $this->assign("sele", $child);
        $this->display();
    }

    public function empl()
    {
        $salestaff = M("salestaff");

        $child = getpages($salestaff, "ssa_agid=" . $this->uinfos['sag_id'], 10);
        $this->assign("list", $child);
        $this->display();
    }

    public function addempls()
    {
        $salestaff = D("salestaff");
        if (!$salestaff->create()) {
            $this->error($salestaff->getError());
        }
        $salestaff->ssa_agid = $this->uinfos["sag_id"];
        if ($salestaff->add()) {
            $this->success("新增成功");
        } else {
            $this->error("新增失败");
        }
    }
}
